﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Data.Common;


namespace Bzure.Common.Transactions
{
    /// <summary>
    /// 事务支持接口
    /// </summary>
    public interface ITransactionSupport
    {
        /// <summary>
        /// 事务上下文
        /// </summary>
        ITransactionContext TransactionContext { get; set; }
        /// <summary>
        /// 创建而不打开连接
        /// </summary>
        /// <returns></returns>
        DbConnection CreateConnection();
        string TransactionName { get; set; }

    }
    /// <summary>
    /// 事务上下文接口
    /// </summary>
    public interface ITransactionContext
    {
        /// <summary>
        /// 事务ID
        /// </summary>
        Guid TransactionID { get; }
        /// <summary>
        /// 数据连接
        /// </summary>
        DbConnection Connection { get; }
        /// <summary>
        /// 数据事务
        /// </summary>
        DbTransaction Transaction { get; }

    }


    /// <summary>
    /// 事务上下文
    /// </summary>
    public class TransactionContext : ITransactionContext
    {
        public TransactionContext()
        {
        }
        public Guid TransactionID { get; set; }
        /// <summary>
        /// 数据连接
        /// </summary>
        public DbConnection Connection { get; set; }
        /// <summary>
        /// 数据事务
        /// </summary>
        public DbTransaction Transaction { get; set; }

    }
    /// <summary>
    /// 事务支持抽象基类
    /// </summary>
    public abstract class TransactionSupportBase : ITransactionSupport
    {
        /// <summary>
        /// 事务上下文
        /// </summary>
        public ITransactionContext TransactionContext { get; set; }
        /// <summary>
        /// 创建而不打开连接
        /// </summary>
        /// <returns></returns>
        public abstract DbConnection CreateConnection();
        public string TransactionName { get; set; }
    }
}
